package com.example.demo.util;


/**
 * <p>Title: Jasine Project</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Intensoft Corp</p>
 *
 * @author seabao
 * @version 1.0
 */

public class ByteUtils {

    /**
     * 遍历字节数组<code>src</code>，但出现和数组<code>seq</code>一致的字节序列时，返回
     * 首字符序号。
     *
     * @param src
     * @param seq
     * @return
     */
    public static final int findSequence(byte[] src, int offset, byte[] seq, int length) {
        int seqoffset = 0;
        final int seqlength = seq.length;

        for (; offset < src.length && seqoffset < seqlength && length > 0; offset++, length--) {
            if (seq[seqoffset] == src[offset]) {
                seqoffset++;
            } else {
                seqoffset = 0;
            }
        }
        if (seqoffset == seqlength) {
            return (offset - seqlength);
        }
        return -1;
    }

    /**
     * 比对两个字符串
     *
     * @param a 待比对字符串
     * @param b 待比对字符串
     * @return 一致返回true, 不一致返回false
     */
    public static final boolean compare(byte[] a, byte[] b) {
        if (a == null && b == null) return true;
        if (a == null || b == null) return false;
        if (a.length != b.length) return false;

        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i])
                return false;
        }
        return true;
    }

    public static final byte[] intToBytes(int input, boolean isBigEndian) {
        byte[] output = new byte[4];
        for (int i = 0; i < 4; i++) {
            int j = isBigEndian ? 3 - i : i;
            output[i] = (byte) ((input >> (j * 8)) & 0xff);
        }
        return output;
    }

    public static final void main(String[] args) {
        byte[] rslt = intToBytes(Integer.valueOf("01101000100110000110100001101000", 2), true);
        System.out.println(Dump.getHexDump(rslt));
    }


}
